/**   
 * @Title: AvroNettyClient.java 
 * @Package com.org.framework.net.avro 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author cssuger@163.com   
 * @date 2017年2月20日 上午10:51:35 
 * @version V1.0   
 */
package com.bicon.base.data.common.net.avro;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.avro.Protocol;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bicon.base.data.common.exchange.AvroUtil;
import com.bicon.base.data.common.log.LogEntity;
import com.bicon.base.data.common.net.Client;



/** 
 *@ClassName: AvroNettyClient 
 *@Description: 
 *@author cssuger@163.com 
 *@date 2017年2月20日 上午10:51:35  
 */
public class AvroNettyClient implements Client{


	
	private Logger loggers = LoggerFactory.getLogger(AvroNettyClient.class);
	
	private String host;
	
	private int port;
	
	public AvroNettyClient(){
		
	}
	
    public AvroNettyClient(String host,int port){
		this.host = host;
		this.port = port;
	}

	@Override
	public void sendLogger(LogEntity logger)  {
		AvroUtil util = new AvroUtil();
		Protocol  protocol = util.parseProtocol();
		if(null == protocol){
			throw new  NullPointerException("双方握手协议不能为空!");
		}
		GenericRecord requestData = new GenericData.Record(protocol.getType("LogEntity"));
		requestData.put("systemName", logger.getSystemName());
		requestData.put("moduleName", logger.getModuleName());
		requestData.put("operateType", logger.getOperateType());
		requestData.put("spendTime", logger.getSpendTime());
		requestData.put("serviceClass", logger.getServiceClass());
		requestData.put("opTime", logger.getOpTime());
		requestData.put("responseMsg", logger.getTip());
		requestData.put("menthodName", logger.getMethodName());
//		Transceiver transceiver = this.connection();
//		GenericRequestor requestor = null;
//		try {
//			GenericRecord request = new GenericData.Record(protocol.getMessages().get("sendLogger").getRequest());//获取请求协议Schema
//			request.put("logentity", requestData);
//			requestor = new GenericRequestor(protocol, transceiver);
//			requestor.request("sendLogger", null, new Callback<Record>(){
//				@Override
//				public void handleResult(Record result) {
//					loggers.info("传输ok");
//				}
//
//				@Override
//				public void handleError(Throwable error) {
//					loggers.info("传输失败,传输数据:");
//				}
//				
//			});
//		} catch (IOException e) {
//			e.printStackTrace();
//		}finally{
//			close(transceiver);
//		}
	}
	
//	private void close(Transceiver transceiver){
//		try{
//			if(null != transceiver){
//				transceiver.close();
//			}
//		}catch(Exception ex){
//			
//		}
//		
//	}
	
	private static class TransceiverThreadFactory implements ThreadFactory {
	    private final AtomicInteger threadId = new AtomicInteger(0);
	    private final String prefix;

	    /**
	     * Creates a TransceiverThreadFactory that creates threads with the
	     * specified name.
	     * @param prefix the name prefix to use for all threads created by this
	     * ThreadFactory.  A unique ID will be appended to this prefix to form the
	     * final thread name.
	     */
	    public TransceiverThreadFactory(String prefix) {
	      this.prefix = prefix;
	    }

	    @Override
	    public Thread newThread(Runnable r) {
	      Thread thread = new Thread(r);
	      thread.setDaemon(true);
	      thread.setName(prefix + " " + threadId.incrementAndGet());
	      return thread;
	    }
	  }

//	@Override
//	public Transceiver connection() {
//		  ExecutorService bossExecutor = Executors.newCachedThreadPool(new TransceiverThreadFactory(
//			          "Avro " + NettyTransceiver.class.getSimpleName() + " Boss"));
//		  ExecutorService workerExecutor = Executors.newCachedThreadPool(new TransceiverThreadFactory(
//			          "Avro " + NettyTransceiver.class.getSimpleName() + " I/O Worker"));
//		  NioClientSocketChannelFactory socketChannelFactory = new NioClientSocketChannelFactory(
//	              bossExecutor, workerExecutor, Runtime.getRuntime().availableProcessors()*2);
//		Transceiver t = null;
//		try {
//			t = new NettyTransceiver(new InetSocketAddress(this.host, this.port),socketChannelFactory, 2000L);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return t;
//
//	}

	
	

	

}
